<?php

namespace app\index\controller;
use app\common\controller\Frontend;
use think\Db;
use think\Request;
use think\Controller;
use wxpay\Notify;
class Wxindex extends Frontend{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    public function index() {  //生成二维码订单，
        
        $apply_id =$this->request->param('apply_id');
        $amount = $this->request->param('amount');
        $data=wechatPay($amount, $apply_id);//调用微信支付方法
        
        $this->assign('data_order',$data);
        
        $this->view->engine->layout('layout/default');
        return $this->view->fetch();
    }

    public function notify() {
        file_put_contents('./time_prev.txt', date("Y-m-d H:i:s"));

        $WeChatNotify = new Notify();
        $xml = file_get_contents('php://input');
        file_put_contents('./cs.txt', $xml . PHP_EOL, FILE_APPEND);
        if ($WeChatNotify->notify($xml) == true) {
            file_put_contents('./time.txt', date("Y-m-d H:i:s"));
            $data = $WeChatNotify->getValues();
            file_put_contents('./data.txt', json_encode($data));
            if (empty($data) || empty($data['out_trade_no']) || empty($data['total_fee'])) {
                return;
            }
            handleOrder($data['out_trade_no'],'paid');//1、更改订单状态
            handleApply($data['out_trade_no']);//2、更改报名状态
            return "SUCCESS";
        }
    }

    public function paylog() {
        $request = Request::instance();
        $data = $request->param();
        if (empty($data) || empty($data['order_number'])) {
            echo 0;
        }
        $order_number = $_GET['order_number'];
        $order = db('hs_order')->where(array('order_number'=>$order_number))->field('status')->find();
        $flag  = $order['status'];
        if($flag === 'paid'){
            echo 1;
        }

    }


    
}
